package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HCarReserveMapper;
import com.flea.hclv.model.dao.HCarReserveUpdateApplyMapper;
import com.flea.hclv.model.entity.HCarReserve;
import com.flea.hclv.model.entity.HCarReserveUpdateApply;
import com.flea.hclv.service.HCarReserveUpdateApplyService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: project
 * @description: 预定车修单申请
 * @author: shiliqiang
 * @create: 2019-02-28 16:01
 * @version: 1.0
 */
@Service
public class HCarReserveUpdateApplyServiceImpl implements HCarReserveUpdateApplyService {

    @Autowired
    private HCarReserveMapper hCarReserveMapper;
    @Autowired
    private HCarReserveUpdateApplyMapper hCarReserveUpdateApplyMapper;

    @Override
    public ServerResponse<FleaPage<HCarReserveUpdateApply>> selectMySendApply(FleaPage<HCarReserveUpdateApply> paging, HttpServletRequest request) {
        RowBounds rowBounds = new RowBounds(paging.getCurPage(),paging.getPageSize());
        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        List<HCarReserveUpdateApply> applies = hCarReserveUpdateApplyMapper.selectMySendApply(rowBounds,deptId);
        FleaPage<HCarReserveUpdateApply> fleaPage = new FleaPage<>(applies);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<String> updateApply(HttpServletRequest request, String model) throws Exception {
        String userId = FleaOrgUtils.getCurUserUuid(request);
//        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        JSONObject jsonObject = JSON.parseObject(model);
        String status;
        if (jsonObject==null){
            status = null;
        }else {
            status = jsonObject.getString("reserveStatus");
            String id = jsonObject.getString("id");
            HCarReserveUpdateApply apply = hCarReserveUpdateApplyMapper.selectByPrimaryKey(id);
            if(apply==null){
                return ServerResponse.createByErrorMessage("申请不存在，可能存在参数错误");
            }
            switch (status) {
                case HConstant.D_CAR_REVERCE_APPLY_NO:
                    apply.setStatus(status);
                    apply.setModifyUser(userId);
                    int row = hCarReserveUpdateApplyMapper.updateByPrimaryKeySelective(apply);
                    if (row > 0) {
                        return ServerResponse.createBySuccessMessage("操作成功");
                    }
                    return ServerResponse.createByErrorMessage("操作失败");
                case HConstant.D_CAR_REVERCE_APPLY_YES:
                    //更新订单
                    HCarReserve reserve = hCarReserveMapper.selectByPrimaryKey(apply.getCarReserveId());
                    if (reserve==null){
                        return ServerResponse.createByErrorMessage("更新的订单不存在");
                    }
                    reserve.setTotalCost(apply.getUpdateTotalCost());
                    reserve.setModifyUser(userId);
                    int row1 = hCarReserveMapper.updateByPrimaryKeySelective(reserve);
                    if (row1 < 0) {
                        return ServerResponse.createByErrorMessage("更新订单失败");
                    }
                    //更新申请
                    apply.setModifyUser(userId);
                    apply.setStatus(HConstant.D_CAR_REVERCE_APPLY_YES);
                    row1 = hCarReserveUpdateApplyMapper.updateByPrimaryKeySelective(apply);
                    if (row1 < 0) {
                        throw new Exception("更新申请状态失败");
                    }
                    return ServerResponse.createBySuccessMessage("操作成功");
                default:
                    return ServerResponse.createBySuccessMessage("操作失败");
            }
        }
        return ServerResponse.createBySuccessMessage("操作失败");
    }

    @Override
    public ServerResponse<FleaPage<HCarReserveUpdateApply>> SelectMyReceiveApply(FleaPage<HCarReserveUpdateApply> paging, HttpServletRequest request, String model) {
        JSONObject jsonObject = JSON.parseObject(model);
        String status;
        if (jsonObject==null){
            status = null;
        }else {
            status = jsonObject.getString("status");
        }

        RowBounds rowBounds = new RowBounds(paging.getCurPage(),paging.getPageSize());
        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        List<HCarReserveUpdateApply> applies = hCarReserveUpdateApplyMapper.selectMyReceiveApply(rowBounds,deptId,status);
        FleaPage<HCarReserveUpdateApply> fleaPage = new FleaPage<>(applies);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<String> addApply(HttpServletRequest request, String model) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        HCarReserve reserve = JSONObject.parseObject(model,HCarReserve.class);
        HCarReserveUpdateApply apply = new HCarReserveUpdateApply();
        apply.setId(FleaStringUtils.getGenerateUUID());
        apply.setCreateUser(userId);
        apply.setSendDeptId(deptId);
        String provideDeptId = reserve.getProvideDeptId();
        apply.setReceiveDeptId(deptId.equals(provideDeptId)?reserve.getUseDeptId(): provideDeptId);
        apply.setStatus(HConstant.D_CAR_REVERCE_APPLY_TO_DO);
        apply.setUpdateTotalCost(reserve.getTotalCost());
        apply.setCarReserveId(reserve.getId());
        int row = hCarReserveUpdateApplyMapper.insertSelective(apply);
        if (row>0){
            return ServerResponse.createBySuccessMessage("提交成功");
        }
        return ServerResponse.createByErrorMessage("提交失败");
    }
}
